
package com.zhu.salesmanagement.views;

import java.util.ArrayList;
import java.util.List;

import com.zhu.location.R;
import com.zhu.salesmanagement.SalesApplication;
import com.zhu.salesmanagement.SyncActivity;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.models.CustomerItem;
import com.zhu.salesmanagement.models.CustomerShelfsItem;
import com.zhu.salesmanagement.models.ProductCategoryItem;
import com.zhu.salesmanagement.models.RemindMsgItem;
import com.zhu.salesmanagement.models.SyncProductItem;
import com.zhu.salesmanagement.models.SyncTaskItem;
import com.zhu.salesmanagement.network.ParseData;
import com.zhu.salesmanagement.network.PostMethod;
import com.zhu.salesmanagement.network.SyncData;
import com.zhu.salesmanagement.network.XMLRPCMethodCallback;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.utils.Util;

import android.content.ContentValues;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SyncView extends LinearLayout implements View.OnClickListener {

    public final static int SYNC_1 = 1;
    public final static int SYNC_2 = 2;
    public final static int SYNC_3 = 3;
    public final static int SYNC_4 = 4;
    public final static int SYNC_5 = 5;
    public final static int SYNC_6 = 6;
    public final static int SYNC_7 = 7;
    public final static int SYNC_SUCCESSEND = 10;
    public final static int SYNC_FAILEDEND = 11;

    private TextView mSyncName = null;
    private TextView mSyncClew = null;
    private TextView mSyncStatus1 = null;
    private TextView mSyncStatus2 = null;
    private TextView mSyncStatus3 = null;
    private TextView mSyncStatus4 = null;
    private TextView mSyncStatus5 = null;
    private TextView mSyncStatus6 = null;
    private TextView mSyncStatus7 = null;

    private ProgressBar mSyncProgressBar1 = null;
    private ProgressBar mSyncProgressBar2 = null;
    private ProgressBar mSyncProgressBar3 = null;
    private ProgressBar mSyncProgressBar4 = null;
    private ProgressBar mSyncProgressBar5 = null;
    private ProgressBar mSyncProgressBar6 = null;
    private ProgressBar mSyncProgressBar7 = null;
    private Button mSyncButton = null;

    private boolean isSyncing = false;
    private int mCurSyncStep = 0;

    private SyncActivity mSyncActivity = null;
    private SyncData mSyncData = null;

    public SyncView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public SyncView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinishInflate() {
        setupViews();
        super.onFinishInflate();
    }

    private void setupViews() {
        mSyncName = (TextView) findViewById(R.id.sync_nameText);
        mSyncClew = (TextView) findViewById(R.id.sync_clewText);
        mSyncButton = (Button)findViewById(R.id.sync_button);
        mSyncButton.setOnClickListener(this);

        findViewById(R.id.sync_back).setOnClickListener(this);
        findViewById(R.id.sync_bottom_back).setOnClickListener(this);

        mSyncStatus1 = (TextView) findViewById(R.id.sync_status_1);
        mSyncStatus2 = (TextView) findViewById(R.id.sync_status_2);
        mSyncStatus3 = (TextView) findViewById(R.id.sync_status_3);
        mSyncStatus4 = (TextView) findViewById(R.id.sync_status_4);
        mSyncStatus5 = (TextView) findViewById(R.id.sync_status_5);
        mSyncStatus6 = (TextView) findViewById(R.id.sync_status_6);
        mSyncStatus7 = (TextView) findViewById(R.id.sync_status_7);

        mSyncProgressBar1 = (ProgressBar) findViewById(R.id.sync_progressBar1);
        mSyncProgressBar2 = (ProgressBar) findViewById(R.id.sync_progressBar2);
        mSyncProgressBar3 = (ProgressBar) findViewById(R.id.sync_progressBar3);
        mSyncProgressBar4 = (ProgressBar) findViewById(R.id.sync_progressBar4);
        mSyncProgressBar5 = (ProgressBar) findViewById(R.id.sync_progressBar5);
        mSyncProgressBar6 = (ProgressBar) findViewById(R.id.sync_progressBar6);
        mSyncProgressBar7 = (ProgressBar) findViewById(R.id.sync_progressBar7);
    }

    public void initData(SyncActivity activity) {
        mSyncActivity = activity;

        mSyncName.setText(getContext().getString(R.string.home_name, Util.mVerifyUserResult.name));
        mSyncClew.setText(R.string.sync_startclew);

        mSyncStatus1.setText(R.string.sync_status_1);
        mSyncStatus2.setText(R.string.sync_status_1);
        mSyncStatus3.setText(R.string.sync_status_1);
        mSyncStatus4.setText(R.string.sync_status_1);
        mSyncStatus5.setText(R.string.sync_status_1);
        mSyncStatus6.setText(R.string.sync_status_1);
        mSyncStatus7.setText(R.string.sync_status_1);
        
        mSyncData = new SyncData(this.getContext(), mHandler);
    }

    private void updateStatus(boolean isEnd, boolean isSuccess) {
        if (mCurSyncStep == SYNC_1) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus1.setText(R.string.sync_status_4);
        			 mSyncProgressBar1.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus1.setText(R.string.sync_status_2);
                mSyncProgressBar1.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_2) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus2.setText(R.string.sync_status_4);
        			 mSyncProgressBar2.setVisibility(View.GONE);
        		}
        	}else{
        		mSyncStatus1.setText(R.string.sync_status_3);
                mSyncStatus2.setText(R.string.sync_status_2);
                mSyncProgressBar1.setVisibility(View.GONE);
                mSyncProgressBar2.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_3) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus3.setText(R.string.sync_status_4);
        			 mSyncProgressBar3.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus2.setText(R.string.sync_status_3);
                mSyncStatus3.setText(R.string.sync_status_2);
                mSyncProgressBar2.setVisibility(View.GONE);
                mSyncProgressBar3.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_4) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus4.setText(R.string.sync_status_4);
        			 mSyncProgressBar4.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus3.setText(R.string.sync_status_3);
                mSyncStatus4.setText(R.string.sync_status_2);
                mSyncProgressBar3.setVisibility(View.GONE);
                mSyncProgressBar4.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_5) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus5.setText(R.string.sync_status_4);
        			 mSyncProgressBar5.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus4.setText(R.string.sync_status_3);
                mSyncStatus5.setText(R.string.sync_status_2);
                mSyncProgressBar4.setVisibility(View.GONE);
                mSyncProgressBar5.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_6) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus6.setText(R.string.sync_status_4);
        			 mSyncProgressBar6.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus5.setText(R.string.sync_status_3);
                mSyncStatus6.setText(R.string.sync_status_2);
                mSyncProgressBar5.setVisibility(View.GONE);
                mSyncProgressBar6.setVisibility(View.VISIBLE);
        	}
        } else if (mCurSyncStep == SYNC_7) {
        	if(isEnd){
        		if(!isSuccess){
        			mSyncStatus7.setText(R.string.sync_status_4);
        			mSyncProgressBar7.setVisibility(View.GONE);
        		}
        	}else{
                mSyncStatus6.setText(R.string.sync_status_3);
                mSyncStatus7.setText(R.string.sync_status_2);
                mSyncProgressBar6.setVisibility(View.GONE);
                mSyncProgressBar7.setVisibility(View.VISIBLE);
        	}
        }else if (mCurSyncStep == SYNC_SUCCESSEND) {
            mSyncStatus7.setText(R.string.sync_status_3);
            mSyncProgressBar7.setVisibility(View.GONE);
            mSyncClew.setText(R.string.sync_startclew);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v != null) {
            switch (v.getId()) {
                case R.id.sync_button:
                    startSync();
                    break;
                case R.id.sync_back:
                case R.id.sync_bottom_back:
                    mSyncActivity.finish();
                    break;
            }
        }
    }

    private void startSync() {
        if(Util.mVerifyUserResult.isOffline){
            Actions.toastShort(getContext(), getContext().getString(R.string.offlineloginnotsync));
            return;
        }
        isSyncing = true;
        mSyncButton.setBackgroundResource(R.drawable.sync_button);
        mSyncClew.setText(R.string.sync_clew);
        mSyncData.startSync();
    }

    private void stopSync(boolean isSuccess) {
        mSyncClew.setText(R.string.sync_startclew);
        mSyncButton.setBackgroundResource(R.drawable.synchronous_blue);
    	isSyncing = false;
    	if(isSuccess){
    		Actions.toastShort(SyncView.this.getContext(), SyncView.this.getContext().getString(R.string.syncsuccess));
    	}else{
    		Actions.toastShort(SyncView.this.getContext(), SyncView.this.getContext().getString(R.string.syncfailed));
    	}
    }

    public boolean isSync() {
        return isSyncing;
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SYNC_1:
                    mCurSyncStep = SYNC_1;
                    updateStatus(false, true);
                    break;
                case SYNC_2:
                    mCurSyncStep = SYNC_2;
                    updateStatus(false, true);
                    break;
                case SYNC_3:
                    mCurSyncStep = SYNC_3;
                    updateStatus(false, true);
                    break;
                case SYNC_4:
                    mCurSyncStep = SYNC_4;
                    updateStatus(false, true);
                    break;
                case SYNC_5:
                    mCurSyncStep = SYNC_5;
                    updateStatus(false, true);
                    break;
                case SYNC_6:
                    mCurSyncStep = SYNC_6;
                    updateStatus(false, true);
                    break;
                case SYNC_7:
                	mCurSyncStep = SYNC_7;
                    updateStatus(false, true);
                    break;
                case SYNC_SUCCESSEND:
                	mCurSyncStep = SYNC_SUCCESSEND;
                	updateStatus(true, true);
                	stopSync(true);
                	break;
                case SYNC_FAILEDEND:
                	updateStatus(true, false);
                	stopSync(false);
                	break;
            }
        }
    };

    public void detory(){
    	if(mSyncData != null){
    		mSyncData.detory();
    		mSyncData = null;
    	}
    }
}
